﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CNotebook;
using CNotebook.Inventory;

namespace NotebookS2
{
    public partial class InventoryRevaluation : Form
    {
        public InventoryRevaluation()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }

        public DataTable LineItems { get; set; }

        void InitializeSeries()
        {

            string _SERIES_CODE = "45";
            SeriesManager oSeries = new SeriesManager();
            oSeries.ConnectionString = this.ConnectionString;
            oSeries.Open();

            if (oSeries.IsAutoNumber(_SERIES_CODE) == true)
            {
                txtDocumentNumber.Properties.ReadOnly = true;
                txtDocumentNumber.Text = "##########";

            }
            oSeries.Close();

        }
        

        string GetNewSeries()
        {
            string _SERIES = "";
            string _DOCTYPE = "45";

            SeriesManager oSeries = new SeriesManager();
            oSeries.ConnectionString = this.ConnectionString;
            oSeries.Open();
            if (oSeries.IsAutoNumber(_DOCTYPE) == true)
            {
                _SERIES = oSeries.GetNewSeries(_DOCTYPE);
            }
            else
            {
                _SERIES = txtDocumentNumber.Text;
            }
            oSeries.Close();

            return _SERIES;
        }


        void RemoveBlankLastRowFirst()
        {
            try
            {

                gridView1.MoveLast();


                string sItemCode = "";


                sItemCode = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["ItemCode"]).ToString();

                if (sItemCode.Trim() == "")
                {
                    gridView1.DeleteRow(gridView1.FocusedRowHandle);
                }
            }
            catch
            {
            }

        }

        void BindWarehouse()
        {
            WarehouseManager oManager = new WarehouseManager();
            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            DataTable oTable = new DataTable();

            oTable = oManager.GetWarehouse();



            
            cboWarehouse.DataSource = oTable;
            cboWarehouse.DisplayMember = "Dscription";
            cboWarehouse.ValueMember = "Code";
            DevExpress.XtraEditors.Controls.LookUpColumnInfo col;

            col = new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Dscription", "Dscription", 100);

            col.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;

            cboWarehouse.Columns.Add(col);
            cboWarehouse.ShowHeader = false;
            
            oManager.Close();
        }

        public void InitTable()
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("WhseCode", typeof(string));
            dt.Columns.Add("ItemCode", typeof(string));
            dt.Columns.Add("ItemName", typeof(string));
            dt.Columns.Add("CurrentCost", typeof(double));
            dt.Columns.Add("NewCost", typeof(double));

            this.LineItems = dt;
        }

        private void InventoryRevaluation_Load(object sender, EventArgs e)
        {
            dtPostDate.EditValue = System.DateTime.Now;
            InitializeSeries();
            InitTable();

            grdCntrlLineItems.DataSource = this.LineItems;
            grdCntrlLineItems.Refresh();

            BindWarehouse();
            if (this.LineItems.Rows.Count == 0)
            {
                gridView1.AddNewRow();
            }
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                if (gridView1.IsLastRow)
                {
                    string sItemCode = "";
                    int iCount = gridView1.FocusedRowHandle;

                    sItemCode = gridView1.GetRowCellValue(iCount, gridView1.Columns["ItemCode"]).ToString();

                    if (sItemCode != "")
                    {
                        gridView1.AddNewRow();

                        gridView1.FocusedRowHandle = DevExpress.XtraGrid.GridControl.NewItemRowHandle;
                        gridView1.FocusedColumn = gridView1.VisibleColumns[0];
                        gridView1.ShowEditor();
                    }
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            RevaluationManager oManager = new RevaluationManager();
            RevaluationUnit oUnit = new RevaluationUnit();

            RemoveBlankLastRowFirst();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            txtDocumentNumber.Text = GetNewSeries();
            oUnit.DocumentNumber = txtDocumentNumber.Text;
            oUnit.PostingDate = Convert.ToDateTime(dtPostDate.EditValue);
            oUnit.Remarks = txtRemarks.Text;

            oUnit.LineItems = this.LineItems;

            oManager.Add(oUnit);

            oManager.Close();

            btnAdd.Enabled = false;

            txtDocumentNumber.Properties.ReadOnly = true;
            txtRemarks.Properties.ReadOnly = true;
            dtPostDate.Enabled = false;

            gridView1.OptionsBehavior.Editable = false;

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtItemCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            NotebookS2.Inventory.Dialogs.FND_REVALITEM oForm = new Inventory.Dialogs.FND_REVALITEM();

            oForm.ConnectionString = this.ConnectionString;
            oForm.ShowDialog();

            gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["ItemCode"], oForm.SelectedItemCode);
            gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["ItemName"], oForm.SelectedItemName);
            gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["CurrentCost"], oForm.SelectedUnitCost);
            gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["NewCost"], 0);
            gridView1.FocusedColumn = gridView1.Columns["NewCost"];
            gridView1.ShowEditor();

        }
    }
}
